package com.xuan.core.service.pg.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuan.core.dto.pg.PgMeiHuaDto;
import com.xuan.core.mapper.pg.PgMeiHuaMapper;
import com.xuan.core.pojo.pg.PgMeiHua;
import com.xuan.core.service.pg.PgMeiHuaService;
import com.xuan.core.service.sys.SysCountService;
import com.xuan.core.vo.pg.PgMeiHuaVo;
import com.xuan.utils.pg.meihua.MeiHua;
import com.xuan.utils.pg.meihua.MeiHuaSetting;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 梅花易数起卦Impl
 */
@Service
public class PgMeiHuaServiceImpl extends ServiceImpl<PgMeiHuaMapper, PgMeiHua> implements PgMeiHuaService {

    @Autowired
    private SysCountService sysCountService; // 系统统计Service

//********************************************************************************************************************************

    /**
     * 开始起卦
     *
     * @param dto 梅花易数起卦数据接收类
     * @return 起卦结果
     */
    @Override
    public PgMeiHuaVo starts(PgMeiHuaDto dto) {

        // 1、拷贝梅花易数起卦设置
        MeiHuaSetting setting = new MeiHuaSetting();
        BeanUtils.copyProperties(dto, setting);

        // 2、梅花易数起卦
        MeiHua meiHua = new MeiHua(setting);

        // 3、增加1次总使用数
        sysCountService.saveUsageNumber();

        // 4、封装梅花易数起卦数据并返回
        return pgMeiHuaVoPackage(dto, meiHua);

    }

//================================================================================================================================

    /**
     * 梅花易数起卦数据封装
     *
     * @param dto    梅花易数起卦数据接收类
     * @param meiHua 梅花易数起卦
     * @return 起卦结果
     */
    private PgMeiHuaVo pgMeiHuaVoPackage(PgMeiHuaDto dto, MeiHua meiHua) {

        PgMeiHuaVo vo = new PgMeiHuaVo();
        BeanUtils.copyProperties(dto, vo); // 设置的数据
        BeanUtils.copyProperties(meiHua, vo); // 计算的数据
        vo.setSex(dto.getSex() == 0 ? "女" : "男"); // 性别
        return vo;

    }


}
